/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MyTableTest3.java
 *
 * Created on Aug 19, 2008, 3:06:34 PM
 */

package swingtest.table;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableModel;
import swingtest.table.entity.MyEntity;

/**
 *
 * @author dyego
 */
public class MyTableTest3 extends javax.swing.JDialog {

    private java.util.List<swingtest.table.entity.MyEntity>  listOfEntitys = new LinkedList<MyEntity>();
    /** Creates new form MyTableTest3 */
    public MyTableTest3(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initMyComponents();
        initComponents();
        initTheTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        list1 = listOfEntitys;
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Name", "Age", "Born At", "Born Time", "Type", "Language Skill", "Image?"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(3).setCellRenderer(new MyDateCellRenderer());
        jTable1.getColumnModel().getColumn(4).setCellRenderer(new MyTimeCellRenderer());
        jTable1.getColumnModel().getColumn(5).setCellRenderer(new MyEnumCellRenderer());
        jTable1.getColumnModel().getColumn(6).setCellRenderer(new MyImageCellRenderer());

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list1, jTable2);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${id}"));
        columnBinding.setColumnName("Id");
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${name}"));
        columnBinding.setColumnName("Name");
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MyTableTest3 dialog = new MyTableTest3(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private java.util.List list1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private void initMyComponents() {
        for (int i = 0; i < 20; i++) {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_YEAR, i);

            MyEntity entity = new MyEntity();
            entity.setId(i);
            entity.setName("My Name "+i);
            entity.setAge(i+20);
            entity.setBorn(cal.getTime());
            entity.setType(swingtest.table.entity.EntityType.TYPE1);
            listOfEntitys.add(entity);
        }

    }

    private void initTheTable() {
        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();

        int cont = 0;
        for (MyEntity myEntity : listOfEntitys) {
            try {
                String lang = "JAVA";
                if ((cont++ % 2) == 0) {
                    lang = "NONE";
                }
                dtm.addRow(new Object[]{myEntity.getId(), myEntity.getName(), myEntity.getAge(), myEntity.getBorn(), myEntity.getBorn(), myEntity.getType(), lang, new ImageIcon(new URL("http://www.virtualvision.ca/images/icon_java.gif"))});
            } catch (MalformedURLException ex) {
                Logger.getLogger(MyTableTest3.class.getName()).log(Level.SEVERE, null, ex);
            }
         }

        jTable1.setDefaultRenderer(java.util.Date.class, new MyDateCellRenderer());

    }

}
